<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Item extends MX_Controller
{
    public function __construct()
    {
        
        $this->load->library('Admin_Layout');
        $this->load->library('Minifier');
        $this->load->model('item_model');
        $this->load->model('product_model');
        $this->load->library('Myvietstring');
    }
    /**
    * add item
    * 
    * 
    */
    public function add()
    {
//        debug (gd_info());
        $pass = 0;
        $mainType = 1;
        $otherType = 2;
//        $itemId = $this->uri->segment(4, 0);
        $listMainProduct = $this->product_model->getGroupProduct($mainType);
        $listOtherProduct = $this->product_model->getGroupProduct($otherType);
        
        foreach($listMainProduct as $mainProduct) {
            if($mainProduct['parent_id'] == 0) {
                $mainPro = $mainProduct;
            } else {
                $listMainPro[] = $mainProduct;
            }            
        }
        $data['mainPro'] = $mainPro;
        $data['listMainPro'] = $listMainPro;
        foreach($listOtherProduct as $otherProduct) {
            if($otherProduct['parent_id'] == 0) {
                $otherPro = $otherProduct;
            } else {
                $listPro[] = $otherProduct;
            }            
        }
        $data['otherPro'] = $otherPro;
        $data['listOtherPro'] = $listPro;    
            
        $data['listMainProduct'] = $listMainProduct;
        $data['listOtherProduct'] = $listOtherProduct;
        
//        $arrayItem = $this->item_model->getItemById($itemId);
        
        if($this->input->post('submit')) {
            //insert database
            $categoryId = $this->input->post('category_id');
            $name_vn = $this->input->post('name_vn');
            $name_en = $this->input->post('name_en');
            $name_jp = $this->input->post('name_jp');
            
            $des_vn = $this->input->post('des_vn');
            $des_en = $this->input->post('des_en');
            $des_jp = $this->input->post('des_jp');
            
            $content_vn = $this->input->post('details_vn');
            $content_en = $this->input->post('details_en');
            $content_jp = $this->input->post('details_jp');
            
            
            $fileUrl = $this->input->post('file_src');
            
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$fileUrl;
            $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/thumb_'.$fileUrl;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 250;
            $config['height'] = 200;
            

            $this->load->library('image_lib', $config);
            $this->image_lib->initialize($config); 

            $this->image_lib->resize();
            if ( ! $this->image_lib->resize())
            {
                echo $this->image_lib->display_errors();
            }
            
            if($this->input->post('checkSet')) {
                $note_id = 1;
            } else $note_id = 0;
            
            $order = $this->input->post('order_id');
            //edit item-----
            
            $data = array();
            
            $data['product_id'] = $categoryId;
            $data['name_vn'] = $name_vn;
            $data['name_en'] = $name_en;
            $data['name_jp'] = $name_jp;
            
            $data['image_src'] = '/uploads/thumb_'.$fileUrl;
            $data['content_vn'] = $content_vn;
            $data['content_en'] = $content_en;
            $data['content_jp'] = $content_jp;
            
            $data['summary_vn'] = $des_vn;
            $data['summary_en'] = $des_en;
            $data['summary_jp'] = $des_jp;
            
            $data['note_id'] = $note_id;
            $data['order_id'] = $order;
            //edit item
            $itemId = $this->item_model->insert($data);
            
            redirect("admin/item/edit/$itemId");
            
        }
        $this->minifier->AddJsNoPack('ckeditor/ckeditor');
        $this->minifier->AddJsNoPack('ckeditor/adapters/jquery');
        $this->minifier->AddJs('uploadify/jquery.uploadify-3.1.min');
        $this->minifier->AddCss('uploadify');
        $this->admin_layout->header();
        $this->admin_layout->leftMenu();

        $this->load->view('item/add_item_view', $data);
    }
    /**
    * edit item + list item
    * 
    * 
    */
    
    public function edit()
    {
        $pass = 0;
        $mainType = 1;
        $otherType = 2;
        $itemId = $this->uri->segment(4, 0);
        $listMainProduct = $this->product_model->getGroupProduct($mainType);
        $listOtherProduct = $this->product_model->getGroupProduct($otherType);
        
        foreach($listMainProduct as $mainProduct) {
            if($mainProduct['parent_id'] == 0) {
                $mainPro = $mainProduct;
            } else {
                $listMainPro[] = $mainProduct;
            }            
        }
        $data['mainPro'] = $mainPro;
        $data['listMainPro'] = $listMainPro;
        foreach($listOtherProduct as $otherProduct) {
            if($otherProduct['parent_id'] == 0) {
                $otherPro = $otherProduct;
            } else {
                $listPro[] = $otherProduct;
            }            
        }
        $data['otherPro'] = $otherPro;
        $data['listOtherPro'] = $listPro;    
            
        $data['listMainProduct'] = $listMainProduct;
        $data['listOtherProduct'] = $listOtherProduct;
        
        $arrayItem = $this->item_model->getItemById($itemId);
        
        if($this->input->post('submit')) {
            //insert database
            $categoryId = $this->input->post('category_id');
            $name_vn = $this->input->post('name_vn');
            $name_en = $this->input->post('name_en');
            $name_jp = $this->input->post('name_jp');
            
            $des_vn = $this->input->post('des_vn');
            $des_en = $this->input->post('des_en');
            $des_jp = $this->input->post('des_jp');
            
            $conten_vn = $this->input->post('details_vn');
            $content_en = $this->input->post('details_en');
            $content_jp = $this->input->post('details_jp');
            
            $fileUrl = $this->input->post('file_src');
            
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$fileUrl;
            $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/thumb_'.$fileUrl;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 200;
            $config['height'] = 200;

            $this->load->library('image_lib', $config);

            $this->image_lib->crop();
            
            if($this->input->post('checkSet')) {
                $note_id = 1;
            } else $note_id = 0;
            
            $order = $this->input->post('order_id');
            //edit item-----
            
            $data = array();
            $data['id'] = $itemId;
            $data['product_id'] = $categoryId;
            $data['name_vn'] = $name_vn;
            $data['name_en'] = $name_en;
            $data['name_jp'] = $name_jp;
            
            $data['image_src'] = '/uploads/thumb_'.$fileUrl;
            
            $data['content_vn'] = $content_en;
            $data['content_en'] = $content_en;
            $data['content_jp'] = $content_jp;
            
            $data['summary_vn'] = $des_vn;
            $data['summary_en'] = $des_en;
            $data['summary_jp'] = $des_jp;
            
            $data['note_id'] = $note_id;
            $data['order_id'] = $order;
            
            //edit item
            $this->item_model->update($data);
            
            redirect('admin/category/index');
            
        }
        $this->minifier->AddJsNoPack('ckeditor/ckeditor');
        $this->minifier->AddJsNoPack('ckeditor/adapters/jquery');
        $this->minifier->AddJs('uploadify/jquery.uploadify-3.1.min');
        $this->minifier->AddCss('uploadify');

        $this->admin_layout->header();
        $this->admin_layout->leftMenu();
        
        $data['item'] = $arrayItem;

        $this->load->view('item/edit_item_view', $data);
    }
    
    public function delete()
    {
        $itemId = $this->uri->segment(4, 0);
        $productId = $this->uri->segment(5,0);
        $this->item_model->delete($itemId);
        redirect("admin/category/listProduct/$productId");

    }

}

?>
